<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>爆款/优选</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-search">
      <span>卖场：</span>
      <el-select
        style="margin-right:20px;"
        v-model="marketId"
        clearable
        @change="selectMarket"
        filterable
        placeholder="请选择卖场"
      >
        <el-option
          v-for="item in marketList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-row>
        <el-select v-model="product_type" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-input
          style="width: 200px;margin: 0 10px"
          placeholder="请输入商品名称关键字"
          v-model="keyword"
          clearable
          @clear="queryList"
        >
        </el-input>
        <el-button type="primary" @click="queryList" >搜索</el-button>
      </el-row>
    </div>
    <div class="table-content">
      <div class="table-bar">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="products"
        tooltip-effect="dark"
        style="width: 100%"

      >
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="product_id" label="序号" width="60"></el-table-column>
        <el-table-column prop="product_type" label="产品类型">
          <template slot-scope="scope">
            <p v-if="scope.row.product_type == 1"> <el-tag type="danger">爆款</el-tag></p>
            <p v-else-if="scope.row.product_type == 2"> <el-tag type="success">优选</el-tag></p>
          </template>
        </el-table-column>
        <el-table-column prop="product_title" label="名称"></el-table-column>
        <el-table-column prop="store_name" label="商家"></el-table-column>
        <el-table-column prop="price" label="销售价"></el-table-column>
        <el-table-column prop="cost_price" label="成本价"></el-table-column>
        <el-table-column prop="primary_price" label="原价"></el-table-column>
        <el-table-column prop="volume" label="销量"></el-table-column>
        <el-table-column prop="num" label="库存"></el-table-column>
        <el-table-column label="操作" fixed="right" width="150">
          <template slot-scope="scope">
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="确定移除该商品吗？"
              @confirm="handleDelete(scope.index,scope.row)"
            >
              <el-button slot="reference" type="danger" size="mini">移除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        product_type:'',
        types:[
          {
            label:'产品类型',
            value:''
          },
          {
            label:'爆款',
            value:'1'
          },
          {
            label:'优选',
            value:'2'
          }
        ],//产品类型
        products: [],
        multipleSelection: [],
        marketId: "", //卖场id
        marketList: [],
      }
    },
    mounted() {
      this.queryList();
      this.remoteMethod();
    },
    methods: {
      // 获取卖场列表
      remoteMethod() {
        this.$http.post("/api/root/Market/marketList").then((res) => {
          console.log(res, "卖场列表");
          if (res.code == 1) {
            this.marketList = res.data ? res.data : [];
          }
        });
      },
      // 选择卖场
      selectMarket() {
        this.page = 1;
        this.pageSize = 10;
        this.queryList();
      },

      handleAdd() {
        this.$router.push({name: 'addProductHot'})
      },

      handleDelete(index, row) {
        this.toDelete(row.id);
      },
      handleSizeChange(val) {
        this.types = [];
        this.pageSize = val;
        this.queryList()
      },
      handleCurrentChange(val) {
        this.types = [];
        this.page = val;
        this.queryList();
      },
      queryList() {
        this.$http.get('/api/root/ProductHot/index', {
          page: this.page,
          pagesize: this.pageSize,
          keyword: this.keyword,
          product_type:this.product_type,
          market_id:this.marketId
        }).then(rest => {
          if (rest.data) {
            this.products = rest.data.products;
            this.total = rest.data.count;
          }
        })
      },
      toDelete(id) {
        const _ths = this;
        this.$http.get('/api/root/ProductHot/del', {
          id_lst: id,
        }).then(rest => {
          if (rest.msg === 'ok') {
            this.$message.success('移除成功');
            setTimeout(function () {
              _ths.queryList();
            }, 600)
          } else {
            this.$message.error(rest.msg);
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
